package com.kzd.btgame.jetpack.api

import com.blankj.utilcode.util.TimeUtils
import com.kzd.btgame.app.*
import com.kzd.btgame.entity.*
import com.kzd.btgame.main.home.adapter.HOME_TRAVEL_TYPE_0
import com.kzd.btgame.main.home.adapter.HOME_TRAVEL_TYPE_1
import com.kzd.btgame.main.home.adapter.HOME_TRAVEL_TYPE_2
import com.kzd.btgame.main.home.adapter.HOME_TRAVEL_TYPE_3
import com.module.lemlin.app.OwnerApplication
import com.module.lemlin.http.*
import rxhttp.RxHttp
import rxhttp.delay
import rxhttp.toClass
import top.zibin.luban.Luban
import java.io.IOException

/**
 * 文件上传
 * */
suspend fun newUploadFile(
    fileName: String? = null, images: MutableList<String>,
): HttpResponseDataE<String> {
    val files = try {
        Luban.with(OwnerApplication.application).load(images).get()
    } catch (e: IOException) {
        null
    }?.map { it.path }?.toMutableList()

    return RxHttp.postForm(NEW_APPS_UPLOAD_IMGS)
        .add("fileName", fileName)
        .addFiles("image[]", files ?: images)
        .toClass<HttpResponseDataE<String>>()
        .await()
}

/**
 * 协议
 * */
suspend fun newGetAgreement(type: String): HttpResponseDataT<UserAgree> =
    RxHttp.postForm(NEW_APPS_GET_AGREEMENT)
        .add("type", type)
        .toClass<HttpResponseDataT<UserAgree>>()
        .await()

/**
 * 合并首页+轮播图接口
 * */
suspend fun newIndex(): HttpResponseDataE<HomeIndex> {
    val homeBannerAnyResponse = RxHttp.postForm(NEW_APPS_GET_BANNER)
        .add("ios", 0)
        .toClass<HttpResponseDataE<Any>>()
        .await()
    val homeIndex = HomeIndex(
        type = 100, list = homeBannerAnyResponse.data
    )
    return RxHttp.postForm(NEW_APPS_INDEX)
        .toClass<HttpResponseDataE<HomeIndex>>()
        .await()
        .apply {
            data.add(0, homeIndex)
            val list = data.filter {
                it.type != 0 && it.list.isNullOrEmpty()
            }
            data.removeAll(list)
        }
}

/**
 * 侧边浮标
 * */
suspend fun newGetIndexActivity(): HttpResponseDataE<HomeFab> =
    RxHttp.postForm(NEW_APPS_GET_INDEX_ACTIVITY)
        .toClass<HttpResponseDataE<HomeFab>>()
        .await()

/**
 * 游戏详情-底部猜一猜
 *
 * @param page 分页
 * @param gid 游戏id
 * */
suspend fun newGetDetailList(
    page: String, gid: String,
): HttpResponseDataE<GameBean> = RxHttp.postForm(NEW_APPS_GET_DETAIL_LIST)
    .add("p", page)
    .add("id", gid)
    .toClass<HttpResponseDataE<GameBean>>()
    .await()

/**
 * 游戏评论
 *
 * @param gid 游戏id
 * @param page 分页
 * @param limit 条数
 * */
suspend fun newGetDiscuss(
    gid: String, page: String, limit: String,
): HttpResponseDataE<GameDiscuss> = RxHttp.postForm(NEW_APPS_GET_PINGLUN)
    .add("id", gid)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameDiscuss>>()
    .await()


/**
 * 游戏评论回复
 *
 * @param gid 游戏id
 * @param page 分页
 * @param limit 条数
 * */
suspend fun newSonDiscussList(
    id: String, page: String, limit: String,
): HttpResponseDataE<GameDiscuss> = RxHttp.postForm(NEW_APPS_SON_PINGLUN_LIST)
    .add("id", id)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameDiscuss>>()
    .await()

/**
 * 游戏详情-开服
 * */
suspend fun newGameServer(
    gameId: String, page: String, limit: String,
): HttpResponseDataE<GameService1> = RxHttp.postForm(NEW_APPS_GAME_SERVER)
    .add("id", gameId)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameService1>>()
    .await()

/**
 * 游戏详情-开服提醒
 * */
suspend fun newRemindServer(id: String): ServiceRemindResponse =
    RxHttp.postForm(NEW_APPS_REMIND_SERVER)
        .add("id", id)
        .toClass<ServiceRemindResponse>()
        .await()


/**
 * 游戏详情-福利
 *
 * @param gameId 游戏id
 * */
suspend fun newWelfare(gameId: String): HttpResponseDataT<GameWelfare> =
    RxHttp.postForm(NEW_APPS_FULI)
        .add("game_id", gameId)
        .toClass<HttpResponseDataT<GameWelfare>>()
        .await()

/**
 * 游戏分类-精选
 *
 * @param systemType 1手游2h5
 * @param ios 1=ios2=安卓
 * */
suspend fun newGetSelectedList(
    systemType: String, ios: String,
): HttpResponseDataT<GameFeatured> = RxHttp.postForm(NEW_APPS_GET_SELECTED_LIST)
    .add("system_type", systemType)
    .add("ios", ios)
    .toClass<HttpResponseDataT<GameFeatured>>()
    .await()

/**
 * 搜索结果点击
 *
 * @param gameId 游戏id
 * */
suspend fun newSearchDetailClick(gameId: String): SearchClickResponse =
    RxHttp.postForm(NEW_APPS_SEARCH_DETAIL_CLICK)
        .add("game_id", gameId)
        .toClass<SearchClickResponse>()
        .await()

/**
 * 搜索热门-猜你想搜-列表
 * */
suspend fun newSearchList(): HttpResponseDataE<SearchDefault> {
    val response = RxHttp.postForm(NEW_APPS_SEARCH_LIST)
        .toClass<HttpResponseDataT<SearchHotGuess>>()
        .await()
    val data = response.data
    val defaultList = mutableListOf<SearchDefault>()
    if (!data.hot.isNullOrEmpty()) defaultList.add(
        SearchDefault(name = "热搜游戏", list = data.hot)
    )
    if (!data.guess.isNullOrEmpty()) defaultList.add(
        SearchDefault(name = "猜你想搜", list = data.guess)
    )
    return HttpResponseDataE(data = defaultList).apply {
        message = response.message
        code = response.code
    }
}

/**
 * 消息中心
 * */
suspend fun newMessage(
    page: String, limit: String,
): HttpResponseDataE<UserMessage> = RxHttp.postForm(NEW_APPS_MESSAGE)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<UserMessage>>()
    .await()

/**
 * 全部已读
 * */
suspend fun newMessageRead(): HttpResponseData =
    RxHttp.postForm(NEW_APPS_MESSAGE_READ)
        .toClass<HttpResponseData>()
        .await()

/**
 * 我的评论
 * */
suspend fun newMyDiscuss(
    page: String, limit: String,
): HttpResponseDataE<MineDiscuss> = RxHttp.postForm(NEW_APPS_MY_PINGLUN)
    .add("type", "0")
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<MineDiscuss>>()
    .await()

/**
 * 新游首发-今日首发
 *
 * @param start
 * */
suspend fun newStarterList(start: String): HttpResponseDataT<Travel<GameGroup>> =
    RxHttp.postForm(NEW_APPS_SHOUFA_LIST)
        .add("start", start)
        .toClass<HttpResponseDataT<Travel<GameGroup>>>()
        .await()

/**
 * 新游首发-一周新游top10
 * */
suspend fun newStarterWeekList(): HttpResponseDataE<GameBean> =
    RxHttp.postForm(NEW_APPS_SHOUFA_WEEK_LIST)
        .toClass<HttpResponseDataE<GameBean>>()
        .await()

/**
 * 新游首发-新游预约
 * */
suspend fun newStarterReserve(
    start: String, page: String, limit: String,
): HttpResponseDataT<Travel<GameReserve>> = RxHttp.postForm(NEW_APPS_YUYUE)
    .add("start", start)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataT<Travel<GameReserve>>>()
    .await()

suspend fun newStarter(): HttpResponseDataE<GameTravel> {
    val todayResponse = newStarterList(start = "0")
    val weekResponse = newStarterWeekList()
    val reserveResponse = newStarterReserve(start = "0", page = "1", limit = "20")
    if (todayResponse.code != 1 && weekResponse.code != 1 && reserveResponse.code != 1)
        return HttpResponseDataE(mutableListOf(GameTravel())).apply {
            code = todayResponse.code
            message = todayResponse.message
        }

    val list: MutableList<GameTravel> = mutableListOf()

    val week = if (weekResponse.code == 1) {
        weekResponse.data
    } else {
        mutableListOf()
    }
    list.add(GameTravel(
        list = week, title = "一周新游Top10", type = HOME_TRAVEL_TYPE_0
    ))

    val today = if (todayResponse.code == 1) {
        todayResponse.data.list.find {
            it.title == TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-MM-dd"))
        }?.list ?: mutableListOf()
    } else {
        mutableListOf()
    }
    list.add(GameTravel(
        list = today, title = "今日首发", type = HOME_TRAVEL_TYPE_1
    ))

    val reserve = if (reserveResponse.code == 1) {
        reserveResponse.data.list
    } else {
        mutableListOf()
    }
    list.add(GameTravel(
        list = reserve, title = "新游预约", type = HOME_TRAVEL_TYPE_2
    ))

    val recent = if (todayResponse.code == 1) {
        todayResponse.data.list.find {
            it.title != TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-MM-dd"))
        }?.list ?: mutableListOf()
    } else {
        mutableListOf()
    }
    list.add(GameTravel(
        list = recent, title = "近期新游", type = HOME_TRAVEL_TYPE_3
    ))

    return HttpResponseDataE(list.filter {
        it.list.size != 0
    }.toMutableList()).apply {
        code = when {
            todayResponse.code == 1 -> todayResponse.code
            weekResponse.code == 1 -> weekResponse.code
            else -> reserveResponse.code
        }
        message = when {
            todayResponse.code == 1 -> todayResponse.message
            weekResponse.code == 1 -> weekResponse.message
            else -> reserveResponse.message
        }
    }
}

/**
 * 新游首发-新游预约修改
 * */
suspend fun newStarterReserveChange(
    gid: String, type: String,
): ReserveChangeResponse = RxHttp.postForm(NEW_APPS_YUYUE_CHANGE)
    .add("gid", gid)
    .add("type", type)
    .toClass<ReserveChangeResponse>()
    .await()

/**
 * 活动数据
 * */
suspend fun newGameLayout1(
    gameLayout: String, start: String, page: String, limit: String,
): HttpResponseDataT<Doings<GameDoing>> = RxHttp.postForm(NEW_APPS_GAME_LAYOUT1)
    .add("game_layout", gameLayout)
    .add("start", start)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataT<Doings<GameDoing>>>()
    .await()

/**
 * 免费领券
 * */
suspend fun newVoucherGame(
    keyword: String? = null, type: String, page: String, limit: String,
): HttpResponseDataE<GameCoupon> = RxHttp.postForm(NEW_APPS_VOUCHER_YOUXI)
    .add("keyword", keyword)
    .add("type", type)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameCoupon>>()
    .await()

/**
 * 交易详情
 *
 * @param id 游戏id
 * */
suspend fun newTradeInfo(id: String): HttpResponseDataT<BargainDetails> =
    RxHttp.postForm(NEW_APPS_TRADE_INFO)
        .add("id", id)
        .toClass<HttpResponseDataT<BargainDetails>>()
        .await()

/**
 * 游戏礼包
 * */
suspend fun newGift(
    keyword: String, type: String, page: String, limit: String,
): HttpResponseDataE<GamePresent> = RxHttp.postForm(NEW_APPS_LB)
    .add("keyword", keyword)
    .add("type", type)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GamePresent>>()
    .await()

/**
 * 游戏开服-今日开服
 * */
suspend fun newGetTodayServer(): ServiceTitleResponse {

    val todayResponse = RxHttp.postForm(NEW_APPS_GET_TODAY_KF)
        .toClass<HomeServiceTodayResponse>()
        .await()
    val data = todayResponse.data

    val titles = mutableListOf<ServiceTitle>()
    for ((key, value) in data.list) {
        titles.add(ServiceTitle(title = key, list = value))
    }
    return ServiceTitleResponse(
        data = titles
    ).apply {
        times = data.times
        code = todayResponse.code
        message = todayResponse.message
    }
}

/**
 * 游戏开服-即将开服
 * */
suspend fun newGetQuickServer(): HttpResponseDataM<String, MutableList<ServiceTitle>> {

    val quickResponse = RxHttp.postForm(NEW_APPS_ALL_KF_LIST)
        .toClass<HttpResponseDataM<String, ServiceTime>>()
        .await()
    val data = quickResponse.data

    val quickMaps = mutableMapOf<String, MutableList<ServiceTitle>>()
    for ((key, value) in data) {
        val titles = mutableListOf<ServiceTitle>()
        for ((key1, value1) in value.list) {
            titles.add(ServiceTitle(title = key1, list = value1))
        }
        quickMaps[key] = titles
    }
    return HttpResponseDataM(
        data = quickMaps
    ).apply {
        code = quickResponse.code
        message = quickResponse.message
    }

}

/**
 * 游戏开服-历史开服
 * */
suspend fun newGetHistoryService(
    page: String, limit: String,
): HttpResponseDataE<GameService> = RxHttp.postForm(NEW_APPS_KF)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameService>>()
    .await()

/**
 * 充值转游
 * */
suspend fun newGetTurnList(
    keyword: String, page: String, limit: String,
): HttpResponseDataE<GameBean> = RxHttp.postForm(NEW_APPS_GET_TURN_LIST)
    .add("keyword", keyword)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameBean>>()
    .await()

/**
 * 充值转游详情
 * */
suspend fun newGetTurnDetail(game_id: String): HttpResponseDataT<GameTransfer> =
    RxHttp.postForm(NEW_APPS_GET_TURN_DETAIL)
        .add("game_id", game_id)
        .toClass<HttpResponseDataT<GameTransfer>>()
        .await()

/**
 * 消息中心
 * */
suspend fun newGetGameList(
    page: String, limit: String,
): HttpResponseDataE<GameFeedback> = RxHttp.postForm(NEW_APPS_GET_GAME_LIST)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<GameFeedback>>()
    .await()


/**
 * 客服数据
 * */
suspend fun newGetServerInfo(): HttpResponseDataT<UserService> =
    RxHttp.postForm(NEW_APPS_GET_KEFU_INFO)
        .toClass<HttpResponseDataT<UserService>>()
        .await()

/**
 * 获取用户消息和代金卷状态 NEW_APPS_GET_TIPS
 * */
suspend fun newGetTips(delay: Long = 0): HttpResponseDataT<AppTips> =
    RxHttp.postForm(NEW_APPS_GET_TIPS)
        .toClass<HttpResponseDataT<AppTips>>()
        .delay(delay)
        .await()